package com.keyware.kd.service.impl.auditlog;

import com.keyware.kd.repository.entity.auditlog.AuditLog;
import com.keyware.kd.repository.entity.auditlog.UserAuditLog;
import com.keyware.kd.repository.mybatis.itf.auditlog.UserAuditLogMapper;
import com.keyware.kd.service.itf.auditlog.UserAuditLogService;
import com.keyware.kd.utils.Constant;
import com.keyware.utils.DateUtils;
import com.keyware.utils.IdGenerator;
import com.keyware.utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Service("userAuditLogService")
public class UserAuditLogServiceImpl implements UserAuditLogService {
	@Autowired
	private UserAuditLogMapper UserAuditLogMapper;


	@Override
	public int deleteByPrimaryKey(String id) {
		//
		return UserAuditLogMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(UserAuditLog record) {
		record.setProjectSource(Constant.projectName);
		return UserAuditLogMapper.insert(record);
	}

	@Override
	public int insertSelective(UserAuditLog record) {
		return UserAuditLogMapper.insertSelective(record);
	}

	@Override
	public UserAuditLog selectByPrimaryKey(String id) {
		// 
		return UserAuditLogMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<UserAuditLog> selectAll(String userId) {
		return UserAuditLogMapper.selectAll(userId, Constant.projectName);
	}

	@Override
	public List<UserAuditLog> selectUserAuditLogBySelective(UserAuditLog log) {
		return UserAuditLogMapper.selectUserAuditLogBySelective(log);
	}

	@Override
	public int updateByPrimaryKey(UserAuditLog record) {
		// 
		return UserAuditLogMapper.updateByPrimaryKey(record);
	}

	public void insertLog(String logName,String logNameType,String comments,HttpServletRequest request){
		UserAuditLog auditLog = new UserAuditLog();
		auditLog.setLogName(logName);
		auditLog.setLogNameType(logNameType);
		auditLog.setId(IdGenerator.uuid32());
		auditLog.setOperTime(DateUtils.getDateTime());
		HttpSession session = request.getSession();
		String userName=(String) session.getAttribute("userName");
		String departName=(String) session.getAttribute("departName");
		String userId=(String) session.getAttribute("userId");
		auditLog.setIpAddress(IpUtil.getIpAddr(request));
		auditLog.setUserName(userName);
		auditLog.setDeptName(departName);
		auditLog.setComments(comments);
		auditLog.setUserId(userId);
		this.insertSelective(auditLog);
		//AuditLog selectByPrimaryKey = UserAuditLogMapper.selectByPrimaryKey(IdGenerator.uuid32());
		//if ("".equals(selectByPrimaryKey) || selectByPrimaryKey == null) {
		//	this.insert(auditLog);
		//}
	}

	@Override
	public int deleteAudit(List listId) {
		return UserAuditLogMapper.deleteAudit(listId);
	}

	@Override
	public List<UserAuditLog> safeAuditLog(String startDate,String endDate) {
		return UserAuditLogMapper.safeAuditLog(startDate, endDate,Constant.projectName);
	}

	@Override
	public List<UserAuditLog> safeSecretAuditLog(String startDate,String endDate) {
		// 
		return UserAuditLogMapper.safeSecretAuditLog(startDate, endDate, Constant.projectName);
	}

	@Override
	public List<UserAuditLog> selectAdminLog(String startDate,String endDate) {
		return UserAuditLogMapper.selectAdminLog(startDate, endDate,Constant.projectName);
	}

	@Override
	public List<UserAuditLog> selectAudit(List<String> listId) {

		return UserAuditLogMapper.selectAudit(listId);
	}

	@Override
	public List<UserAuditLog> selectAllLog(String startDate,String endDate) {
		return UserAuditLogMapper.selectAllLog(startDate, endDate,Constant.projectName);
	}

	@Override
	public List<UserAuditLog> selectLogUserName() {
		return UserAuditLogMapper.selectLogUserName();
	}

}
